<template>
  <el-row type="flex" class="row-bg" justify="center">
    <div class="title-container">
      <h3 class="title">欢迎来到浩泽管理软件后台系统test</h3>
    </div>
    <el-col :xl="6" :lg="7">
      <el-card shadow="always" class="card">
        <el-form
          :model="loginForm"
          :rules="rules"
          ref="loginForm"
          label-width="80px"
          class="form_fa"
          @keyup.enter.native="submitForm('loginForm')"
        >
          <el-form-item label="账号" prop="empno" style="width: 100%">
            <el-input v-model="loginForm.empno" placeholder="账号"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password" style="width: 100%">
            <el-input v-model="loginForm.password" type="password" placeholder="密码">
			</el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('loginForm')" v-debounce
				style="width: 100%">
				登录
			</el-button>
          </el-form-item>

          <!-- <el-form-item>
            <el-button type="primary" @click="changePassword" style="width: 100%">修改密码</el-button>
          </el-form-item> -->
        </el-form>
      </el-card>
    </el-col>
  </el-row>
</template>

<script>
import { Notification } from "element-ui";
import { SA_TOKEN_NAME, TOKEN_KEY } from "../constant/system";

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        empno: "",
        password: "",
      },
      rules: {
        empno: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      captchaImg: undefined,
    };
  },
  created() {
	  let loginUser = JSON.parse(localStorage.getItem("userInfo"));
	  if (loginUser){
		  this.loginForm.empno = loginUser.nickName;
	  }
  },
  methods: {
    changePassword() {
      this.$router.push("/changePassword");
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post("/user/login", this.loginForm).then((res) => {
            //console.log("res==>",res)
            if (res.code === 200) {
              const token = res.data.OaAuth;

              // 存储token
              this.$store.commit("SET_TOKEN", token);
              // 存储用户信息
              this.$store.commit("SET_USER", res.data.loginUser);
			  localStorage.setItem("userInfo",JSON.stringify(res.data.loginUser));
              //把角色id存入到 localStorage
              localStorage.setItem("role", res.data.loginUser.roleId);

              // window.sessionStorage.setItem(
              //     "loginUser",
              //     JSON.stringify(res.data.loginUser)
              // );

              Notification.success("登录成功");
              this.$router.push({ path: "/index" });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.form_fa >>> .el-form-item__label {
  color: #fff;
}

.el-button {
  width: 130px;
  height: 40px;
  font-size: 1em;
  font-weight: bold;
  background-color: #293140;
  border: 3px #3bb6f4 solid;
  margin: auto;
  color: #3bb6f4;
}

.el-button:hover {
  background-color: #156cd2;
  color: #fff;
}

.card {
  background-color: rgba(130, 187, 232, 0.1);
  /* opacity: .1; */
  border: none;
  backdrop-filter: blur(100px);
}

.title {
  font-size: 26px;
  color: white;
  margin: 0px 30px 40px 30px;
  text-align: center;
  font-weight: bold;
  display: inline-block;
}

.el-row {
  background-image: url("../assets/back.png");
  background-size: 100%;
  background-repeat: no-repeat;
  color: #fff;
  height: 100%;
  display: flex;
  align-items: center;
  text-align: center;
  justify-content: center;
}

.captchaImg {
  float: left;
  margin-left: 8px;
  border-radius: 4px;
}
</style>
